﻿#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Timers;
using System.Collections;
using Microsoft.VisualBasic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BrainPong
{
    public class Bat
    {
        #region Variable Declaration
        private Vector3 _position;
        private static Model _model;
        private Vector3 _rotation;
        public KeyboardState curKeyState, prevKeyState;
        private float _speed = 1.5f;
        private Vector3 _scale = new Vector3( 1f , 0.5f , 1f);
        public float height = 10.0f;
        public float width = 5.0f;
        public int points = 0;
        #endregion

        public Bat(Vector3 position)
        {
            _position = position;
            _rotation = new Vector3( 0 ,  0 , (float) Math.PI * 0.5f);
        }

        public static void loadModel()
        {
            _model = Game1.game.loadModel("bar2");
        }

        public void UpdateInput(Keys up, Keys down)
        {
            prevKeyState = curKeyState;
            curKeyState = Keyboard.GetState();

            if (curKeyState.IsKeyDown(up) && checkNewPosition(Vector3.Up))
            {
                _position += Vector3.UnitY * _speed;
            }
            if (curKeyState.IsKeyDown(down) && checkNewPosition(Vector3.Down))
            {
                _position -= Vector3.UnitY * _speed;
            }
        }

        public Vector3 getPosition()
        {
            return _position;
        }

        private Boolean checkNewPosition(Vector3 direction)
        {
            float ymax = Game1.veld.YMax();
            float ymin = Game1.veld.YMin();

            float yMax = (_position + direction * _speed).Y + height / 2;
            float yMin = (_position + direction * _speed).Y - height / 2;


            
            Boolean YMAX = yMax < ymax;
            Boolean YMIN = yMin > ymin;

            return (YMAX && YMIN);
        }


        public void draw(Game1 game)
        {
            game.drawModel(_model, _position, _rotation, _scale);
        }
    }
}
